package com.tencent.xinyapayface.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.tencent.xinyapayface.bean.OkObject;

import java.io.File;


/**
 * Created by Administrator on 2017/8/27.
 */
public class ApiClient {
    public final static int OK = 200;
    public final static int OUT = 403;//token失效
    public final static int OTHER = 405;//其他设备登录
    //    public final static int FAIL = 0;
    public final static String md5Key = "test123";
    public final static String secret_key = "1234567890123456";
    public final static String iv = "1234567890123456";

    public interface CallBack {
        void onSuccess(String s);

        void onError();
    }

    public interface UpLoadCallBack {
        void onSuccess(String s);

        void onError();

        void uploadProgress(float progress);
    }


    public static void postNormal(Context context, final OkObject okObject, final CallBack callBack) {
        LogUtil.LogShitou("ApiClient--post", "" + okObject.getUrl());
        LogUtil.LogShitou("ApiClient--发送", "" + okObject.getJson());
        HttpHeaders httpHeaders = okObject.getHttpHeaders();
        if (httpHeaders == null) {
            httpHeaders = new HttpHeaders();
        }
        OkGo.<String>post(okObject.getUrl())
                .tag(context)
                .headers(httpHeaders)
                .params(okObject.getParams())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callBack.onSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callBack.onError();
                        LogUtil.LogShitou("ApiClient--onErrorbody", "" + okObject.getUrl());
                        LogUtil.LogShitou("ApiClient--onErrorcode", "" + response.code());
                        LogUtil.LogShitou("ApiClient--onErrormessage", "" + response.message());
                        LogUtil.LogShitou("ApiClient--onErrorgetException", "" + response.getException().toString());
                    }
                });
    }

    public static void postJson(Context context, String url, String json, final CallBack callBack) {
        LogUtil.LogShitou("ApiClient--发送", "" + json);
        OkGo.<String>post(url)
                .tag(context)
                .upJson(json)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        callBack.onSuccess(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        callBack.onError();
                    }
                });
    }



    public static void cancleAll() {
        OkGo.getInstance().cancelAll();
    }

    public static void cancleThisPage(Context context) {
        OkGo.getInstance().cancelTag(context);
    }

    /**
     * 下载文件
     *
     * @param context
     * @param url
     */
    public static void downLoadFile(final Context context, String url, String dir, String fileName, final CallBack callBack) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                OkGo.<File>get(url)
                        .tag(context)
                        .execute(new FileCallback(dir, fileName) {
                            @Override
                            public void onSuccess(Response<File> response) {
                                callBack.onSuccess(response.body().toString());
                            }

                            @Override
                            public void onError(Response<File> response) {
                                super.onError(response);
                                LogUtil.LogShitou("ApiClient--onErrorbody", "" + response.body());
                                LogUtil.LogShitou("ApiClient--onErrorcode", "" + response.code());
                                LogUtil.LogShitou("ApiClient--onErrormessage", "" + response.message());
                                LogUtil.LogShitou("ApiClient--onErrorgetException", "" + response.getException().toString());
                                callBack.onError();
                            }
                        });
            } catch (Exception e) {
            }
        } else {
            Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 下载文件
     *
     * @param context
     * @param url
     */
    public static void downLoadImg(final Context context, String url, String dir, final String fileName, final CallBack callBack) throws Exception {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            final String filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + dir;
            OkGo.<File>get(url)
                    .tag(context)
                    .execute(new FileCallback(filePath, fileName) {
                        @Override
                        public void onSuccess(Response<File> response) {
                            LogUtil.LogShitou("ApiClient--onSuccess", "" + response.body().toString());
                            callBack.onSuccess(response.body().toString());
                            // 最后通知图库更新
                            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                    Uri.fromFile(new File(filePath, fileName))));
                        }

                        @Override
                        public void onError(Response<File> response) {
                            super.onError(response);
                            LogUtil.LogShitou("ApiClient--onErrorbody", "" + response.body());
                            LogUtil.LogShitou("ApiClient--onErrorcode", "" + response.code());
                            LogUtil.LogShitou("ApiClient--onErrormessage", "" + response.message());
                            LogUtil.LogShitou("ApiClient--onErrorgetException", "" + response.getException().toString());
                            callBack.onError();
                        }
                    });
        } else {
            Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
        }

    }

}
